package com.insigma.business.qrcode.soldier._provider.impl;

import com.alibaba.fastjson.JSONObject;
import com.insigma.business.qrcode.common.dto.SystemConfigDTO;
import com.insigma.business.qrcode.common._consumer.SystemConfigConsumer;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.CommonConstant;
import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.AliPayFaceRecognitionBaseRequest;
import com.insigma.business.qrcode.soldier.dto.FaceRecognitionDTO;
import com.insigma.business.qrcode.soldier.dto.LoggedInDTO;
import com.insigma.business.qrcode.soldier.dto.PlacementSoldierInfoDTO;
import com.insigma.business.qrcode.soldier.request.AlipayLifeOauth2UrlGetRequest;
import com.insigma.business.qrcode.soldier.request.AlipayXcxOAuth2Request;
import com.insigma.business.qrcode.soldier.request.ModifyPwdDirectlyRequest;
import com.insigma.business.qrcode.soldier._consumer.PlacementSoldierInfoConsumer;
import com.insigma.business.qrcode.soldier._provider.SoldierSecurityProvider;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SoldierSecurityProviderImpl implements SoldierSecurityProvider {

    @Resource
    PlacementSoldierInfoConsumer placementSoldierInfoConsumer;

    @Resource
    SystemConfigConsumer systemConfigConsumer;

    @Override
    public BaseResponse<String> getAlipayOauth2Url(AlipayLifeOauth2UrlGetRequest alipayLifeOauth2UrlGetRequest) {
        return null;
    }

    @Override
    public BaseResponse<Integer> logout() {
        return null;
    }

    @Override
    public BaseResponse modifyPassword(ModifyPwdDirectlyRequest request) {
        return null;
    }

    @Override
    public BaseResponse<JSONObject> getLoggedIn() {
        return null;
    }

    @Override
    public BaseResponse<LoggedInDTO> getAliXcxToken(AlipayXcxOAuth2Request request) {
        return null;
    }

    @Override
    public BaseResponse<FaceRecognitionDTO> getFaceRecognitionByPlacement() {
        PlacementSoldierInfoDTO soldierInfoDTO = placementSoldierInfoConsumer.getFilteredSoldierDetail(SessionUtils.getSoldierUserId()).getDto();
        BizCheckUtil.checkNotNull(soldierInfoDTO, "查无此人");

        SystemConfigDTO systemConfigDTO = systemConfigConsumer.getSystemConfigByName(CommonConstant.SYSTEM_CONFIG_ALI_FACE_JUMP_URL).getDto();
        BizCheckUtil.checkNotNull(systemConfigDTO, "内部参数错误");
        return getFaceRecognition(new AliPayFaceRecognitionBaseRequest(soldierInfoDTO.getIdCardNo(), soldierInfoDTO.getRealName(), systemConfigDTO.getValue()));    }

    private BaseResponse<FaceRecognitionDTO> getFaceRecognition(AliPayFaceRecognitionBaseRequest aliPayFaceRecognitionBaseRequest) {
//        initAliPayXcxConfig();
//        try {
//            AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient(xcxAppid, xcxPrivateKey, xcxAliPublicKey);
//            AlipayUserCertifyOpenInitializeRequest request = new AlipayUserCertifyOpenInitializeRequest();
//            String id = UUID.randomUUID().toString();
//
//            AlipayFaceRequest aa = new AlipayFaceRequest(param.getRealName(), param.getIdCardNo(), id,
//                    "",
//                    param.getJumpUrl());
//            String s = JSON.toJSONString(aa);
//            request.setBizContent(s);
//            AlipayUserCertifyOpenInitializeResponse response = alipayClient.execute(request);
//            if (response.isSuccess()) {
//                String certifyId = response.getCertifyId();
//                AlipayUserCertifyOpenCertifyRequest certRequest = new AlipayUserCertifyOpenCertifyRequest();
//
//                JSONObject bizContentObj = new JSONObject();
//                bizContentObj.put("certify_id", certifyId);
//                certRequest.setBizContent(bizContentObj.toString());
//
//
//                AlipayUserCertifyOpenCertifyResponse certifyResponse = alipayClient.pageExecute(certRequest, "GET");
//                if (certifyResponse.isSuccess()) {
//                    log.info("开始认证服务调用成功");
//                    String certifyUrl = certifyResponse.getBody();
//                    log.info("人脸识别链接", certifyUrl);
//                    return ResponseUtils.buildSuccessResponse(new FaceRecognitionDTO(id, certifyId, certifyUrl));
//                } else {
//                    log.info("调用失败");
//                }
//            } else {
//                log.info("调用失败");
//            }
//        } catch (AlipayApiException e) {
//            log.error("error:", e);
//        }
//        return null;
//    }
//    private String xcxAppid;
//    private String xcxPrivateKey;
//    private String xcxAliPublicKey;
//    private void initAliPayXcxConfig() {
//
//        if (Strings.isNotBlank(xcxAliPublicKey)) {
//            return;
//        }
//        SystemConfigDTO privatekey = systemConfigConsumer.getSystemConfigByName(CommonConstant.SYSTEM_CONFIG_ALI_XCX_PRIVATE_KEY).getDto();
//        SystemConfigDTO publicKey = systemConfigConsumer.getSystemConfigByName(CommonConstant.SYSTEM_CONFIG_ALI_XCX_PUBLIC_KEY).getDto();
//        SystemConfigDTO appId = systemConfigConsumer.getSystemConfigByName(CommonConstant.SYSTEM_CONFIG_ALI_XCX_APP_ID).getDto();
//        xcxAppid = appId.getValue();
//        xcxPrivateKey = privatekey.getValue();
//        xcxAliPublicKey = publicKey.getValue();
        return new BaseResponse<>();
    }
}
